import axios from 'axios'
import { fromJS} from 'immutable'
import * as constant from './constant'

const changeBoardData = ( result ) => ({
    type: constant.CHANGE_BOARD_DATA,
    articleList: result.articleList,
    writerList: result.writerList
    
})

const addMoreData = ( list, nextPage ) => ({
    type: constant.GET_MORE_ARTICLE,
    //将新加入的数据，里外都转变为immutable对象
    list: fromJS(list),
    nextPage
})
export const getBoardDate = (id) => {
    return (dispatch) => {
            axios.get('/api/board.json?id' + id).then((res) => {
                const result = res.data.data;
                dispatch( changeBoardData(result) );
            })     
   }   
}
export const getMoreList = (page) => {
    return (dispatch) => {
        axios.get('/api/boardList.json?page' + page)
        .then((res) => {
          const result = res.data.data;
          dispatch( addMoreData(result, page+1)); 
        })
    }
}